import React, {Component} from 'react';
import './index.less';
import {base, toast, high} from 'nc-lightapp-front';
import getReptObjData from "../ReptObjModal/functions/getReptObjData";
import getJobHistoryData from "../JobHisModal/functions/getJobHistoryData";
import getExportInitData from "../ExportModal/functions/getExportInitData";
import exportPsnPhoto from "../../container/functions/exportPsnPhoto";
import templatePrint from "../../container/functions/templatePrint";
import ReferSearch from "../../../../../../hrpub/common/components/referSearch/org";
import printOutput from "../../container/functions/printOutput";
import setQueryMeta from "../../container/functions/setQueryMeta";

const {NCCheckbox} = base;
const {PrintOutput} = high;

class Header extends Component {
    constructor(props) {
        super(props);
        this.state = {printData: {}};
        this.updateOrgValue = this.updateOrgValue.bind(this);
        this.onBtnOperation = this.onBtnOperation.bind(this);
        this.getRosterPks = this.getRosterPks.bind(this);
    }

    updateOrgValue(value) {
        this.props.updateOrgValue(value);
    }

    onChangeHandle(type) {
        return (value) => {
            this.props.updateState({
                [type]: value,
                pageInfo: {
                    ...this.props.pageInfo,
                    pageIndex: 1
                }
            }, () => {
                if (this.props.hasSearched) {
                    this.props.getMainTableData();
                }
            });
        }
    }

    getRosterPks() {
        const {table} = this.props;
        let selectedData = table.getCheckedRows('ncc60071702list'), psnjob_pks = '';
        if (!selectedData.length) {
            psnjob_pks = table.getAllTableData('ncc60071702list').rows.map(item => item.values.pk_psnjob.value).join();
        } else {
            psnjob_pks = selectedData.map(item => item.data.values.pk_psnjob.value).join();
        }
        return psnjob_pks;
    }

    onBtnOperation(props, btncode, areacode, opt) {
        switch (btncode) {
            case 'Query':
                setQueryMeta.call(this);
                this.props.search.openAdvSearch('ncc60071702query', true);
                break;
            case 'Sort':
                this.props.updateState({
                    sortModalVisible: true
                });
                break;
            case 'Refresh':
                if (this.props.hasSearched) {
                    this.props.updateState({
                        pageInfo: {
                            ...this.props.pageInfo,
                            pageIndex: 1
                        }
                    }, () => {
                        this.props.getMainTableData();
                    });
                }
                break;
            case 'ListReport':
                const rosterPks = this.getRosterPks();
                this.props.updateState({
                    rosterModalVisible: true,
                    rosterPks
                });
                break;
            case 'cardrpt':
                this.props.viewCard();
                break;
            case 'ReptObj':
                const clickRow = this.props.table.getClickRowIndex('ncc60071702list');
                if (!clickRow) {
                    toast({color: "warning", content: this.props.json['hi6007-000092']});
                    /* 国际化处理： 请选中要处理的数据*/
                } else {
                    getReptObjData.call(this,
                        clickRow.record.pk_psnjob.value,
                        (reptInfo) => {
                            this.props.updateState({
                                reptColumns: reptInfo.columns,
                                reptData: reptInfo.data,
                                reptObjVisible: true
                            });
                        });
                }
                break;
            case 'JobHis':
                const clickRow1 = this.props.table.getClickRowIndex('ncc60071702list');
                if (!clickRow1) {
                    toast({color: "warning", content: this.props.json['hi6007-000092']});
                    /* 国际化处理： 请选中要处理的数据*/
                } else {
                    getJobHistoryData.call(this, clickRow1.record.pk_psnjob.value);
                }
                break;
            case 'Print':
                templatePrint.call(this);
                break;
            case 'output':
                printOutput.call(this);
                break;
            case 'ExportPsnInfo':
                const clickRow3 = this.props.table.getClickRowIndex('ncc60071702list');
                if (!clickRow3) {
                    toast({color: "warning", content: this.props.json['hi6007-000092']});
                    /* 国际化处理： 请选中要处理的数据*/
                } else {
                    getExportInitData.call(this, 'cur');
                }
                break;
            case 'ExportPsnInfoAll':
                getExportInitData.call(this, 'all');
                break;
            case 'ExportPhoto':
                const checkedRows = this.props.table.getCheckedRows('ncc60071702list');
                if (checkedRows.length === 0) {
                    toast({color: "warning", content: this.props.json['hi6007-000060']});
                    /* 国际化处理： 请勾选要处理的数据*/
                } else {
                    exportPsnPhoto.call(this, checkedRows.map(item => item.data.values.pk_psnjob.value).join());
                }

                break;
            case 'File':
                const clickRow2 = this.props.table.getClickRowIndex('ncc60071702list');
                if (!clickRow2) {
                    toast({color: "warning", content: this.props.json['hi6007-000092']});
                    /* 国际化处理： 请选中要处理的数据*/
                } else {
                    this.props.updateState({
                        showUploader: true
                    });
                }
                break;
            default:
                break;
        }
    }

    render() {
        const {
            button, treeType, include_child_depts, include_child_orgs, page,
            include_child_psncl, show_all_info, show_on_job_psn, orgVal, showSubsetDisabled,subsetCardChecked
        } = this.props;

        return (
            <div className="header">
                <div className="header-refer">
                    <ReferSearch
                        orgVal={orgVal}
                        getOrgData={this.updateOrgValue}
                    />
                </div>
                <div className="header-checkbox-group">
                    <NCCheckbox
                        checked={show_on_job_psn}
                        className="header-checkbox"
                        color="primary"
                        onChange={this.onChangeHandle('show_on_job_psn')}
                    >
                        {this.props.json['hi6007-000044']}{/* 国际化处理： 在职人员*/}
                    </NCCheckbox>
                    {treeType === 'adminOrg' ? <NCCheckbox
                        checked={include_child_orgs}
                        className="header-checkbox"
                        color="primary"
                        onChange={this.onChangeHandle('include_child_orgs')}
                    >
                        {this.props.json['hi6007-000045']}{/* 国际化处理： 包含下级组织人员*/}
                    </NCCheckbox> : null}
                    {treeType === 'adminOrg' ? <NCCheckbox
                        checked={include_child_depts}
                        className="header-checkbox"
                        color="primary"
                        onChange={this.onChangeHandle('include_child_depts')}
                    >
                        {this.props.json['hi6007-000046']}{/* 国际化处理： 包含下级部门人员*/}
                    </NCCheckbox> : null}
                    {treeType === 'psnType' ? <NCCheckbox
                        checked={include_child_psncl}
                        className="header-checkbox"
                        color="primary"
                        onChange={this.onChangeHandle('include_child_psncl')}
                    >
                        {this.props.json['hi6007-000093']}{/* 国际化处理： 包含下级人员类别*/}
                    </NCCheckbox> : null}
                    {/*<NCCheckbox
                        className="header-checkbox"
                        color="primary"
                    >
                        显示子集
                    </NCCheckbox>*/}
                    <NCCheckbox
                        checked={show_all_info}
                        className="header-checkbox"
                        color="primary"
                        onChange={this.onChangeHandle('show_all_info')}
                    >
                        {this.props.json['hi6007-000095']}{/* 国际化处理： 包含历史人员*/}
                    </NCCheckbox>
                    <NCCheckbox
                        checked={subsetCardChecked}
                        onChange={(status) => {
                            this.props.updateState({
                                subsetVisible: status,
                                subsetCardChecked: status,
                                isShowBottomDom: status
                            }, () => {
                                if(status) {
                                    setTimeout(() => {
                                        this.props.cIns.subSetCard.initSubSet();
                                    }, 1);
                                }
                            });
                            
                        }}
                        disabled={showSubsetDisabled}
                    >
                        {this.props.json['hi6007-000094']/** 显示子集 */}
                    </NCCheckbox>
                </div>

                <div className="btn-group">
                    {page === 'main' ? <div className="definition-icons">
                        {button.createButtonApp({
                            area: 'head',
                            onButtonClick: (props, btncode) => {
                                this.onBtnOperation(props, btncode, 'head', this);
                            }
                        })}
                    </div> : null}
                </div>

                <PrintOutput
                    ref='printOutput'
                    url={'/nccloud/hrhi/psndoc/PsndocTemplatePrintAction.do'}
                    data={this.state.printData}
                />
            </div>
        );
    }
}

export default Header;
